<?php
namespace Api\Controller;
use Think\Controller;
/**
 * 支付宝
 */
class AlipayController extends Controller{

    public function cert_notify(){
        vendor('Alipay.alipay_notify','','.class.php');
        $alipay_config = C(ALIPAY_CONFIG);
        $alipayNotify = new \AlipayNotify($alipay_config);
        if($alipayNotify->getResponse($_POST['notify_id']))//判断成功之后使用getResponse方法判断是否是支付宝发来的异步通知。
        {
            // if($alipayNotify->getSignVeryfy($_POST, $_POST['sign'])) {//使用支付宝公钥验签

                //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];

                //支付宝交易号
                $trade_no = $_POST['trade_no'];

                //交易状态
                $trade_status = $_POST['trade_status'];

                if($_POST['trade_status'] == 'TRADE_FINISHED') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['certorder_sn'] = $out_trade_no;
                    $certorder = M('certorder')->where($data)->find();
                    //如果订单未支付，则进行数据库修改
                    if ($certorder['certorder_paystatus'] == 1) {
                        $certorder['certorder_paystatus'] = 2;
                        $certorder['certorder_finishtime'] = time();
                        M('certorder')->save($certorder);
                    }
                }
                else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //付款完成后，支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['certorder_sn'] = $out_trade_no;
                    $certorder = M('certorder')->where($data)->find();
                    //如果订单未支付，则进行数据库修改
                    if ($certorder['certorder_paystatus'] == 1) {
                        $certorder['certorder_paystatus'] = 2;
                        $certorder['certorder_finishtime'] = time();
                        M('certorder')->save($certorder);
                    }
                }
                //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                echo "success";     //请不要修改或删除
            // }
            // else //验证签名失败
            // {
            //     echo "sign fail";
            // }
        }
        else //验证是否来自支付宝的通知失败
        {
            echo "response fail";
        }
    }

    public function goods_notify(){
        vendor('Alipay.alipay_notify','','.class.php');
        $alipay_config = C(ALIPAY_CONFIG);
        $alipayNotify = new \AlipayNotify($alipay_config);
        if($alipayNotify->getResponse($_POST['notify_id']))//判断成功之后使用getResponse方法判断是否是支付宝发来的异步通知。
        {
            // if($alipayNotify->getSignVeryfy($_POST, $_POST['sign'])) {//使用支付宝公钥验签

                //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];

                //支付宝交易号
                $trade_no = $_POST['trade_no'];

                //交易状态
                $trade_status = $_POST['trade_status'];

                if($_POST['trade_status'] == 'TRADE_FINISHED') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['order_num'] = $out_trade_no;
                    $order = M('order')->where($data)->find();
                    //如果订单未支付，则进行数据库修改
                    if ($order['order_paystatus'] == 1) {
                        $order['order_paystatus'] = 2;
                        $order['order_endtime'] = time();
                        M('order')->save($order);
                    }
                }
                else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //付款完成后，支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['order_num'] = $out_trade_no;
                    $order = M('order')->where($data)->find();
                    //如果订单未支付，则进行数据库修改
                    if ($order['order_paystatus'] == 1) {
                        $order['order_paystatus'] = 2;
                        $order['order_endtime'] = time();
                        M('order')->save($order);
                    }
                }
                //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                echo "success";     //请不要修改或删除
            // }
            // else //验证签名失败
            // {
            //     echo "sign fail";
            // }
        }
        else //验证是否来自支付宝的通知失败
        {
            echo "response fail";
        }
    }

    public function exam_notify(){
        vendor('Alipay.alipay_notify','','.class.php');
        $alipay_config = C(ALIPAY_CONFIG);
        $alipayNotify = new \AlipayNotify($alipay_config);
        if($alipayNotify->getResponse($_POST['notify_id']))//判断成功之后使用getResponse方法判断是否是支付宝发来的异步通知。
        {
            // if($alipayNotify->getSignVeryfy($_POST, $_POST['sign'])) {//使用支付宝公钥验签

                //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];

                //支付宝交易号
                $trade_no = $_POST['trade_no'];

                //交易状态
                $trade_status = $_POST['trade_status'];

                if($_POST['trade_status'] == 'TRADE_FINISHED') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['exam_num'] = $out_trade_no;
                    $exam = M('exam')->where($data)->find();
                    if ($exam['exam_paystatus'] == 1) { //如果订单未支付
                        $exam['exam_paystatus'] = 2;    //支付成功
                        $exam['exam_status'] = 1;    //报名成功
                        M('exam')->save($exam);    //保存考试信息
                        //考生推送(暂无)
                        //考生通知
                        $user = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_user_id'])->find();

                        if ($exam['exam_midiclub_id'] != -1) {  //如果是线下考试，增加考场人数
                            $condition['examoffline_roomid'] = $exam['exam_midiclub_id'];
                            $condition['examoffline_ymd'] = $exam['exam_ymd'];
                            $offline = M('examoffline')->where($condition)->find();
                            $major = '';
                            if ($exam['exam_major_id'] == 1) {
                                $offline['examoffline_already_guitar'] += 1;
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $offline['examoffline_already_drum'] += 1;
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $offline['examoffline_already_bass'] += 1;
                                $major = '电贝司';
                            }
                            M('examoffline')->save($offline);
                            //考生通知
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].$major.$exam['exam_level'].'级的线下考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线下报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'];
                            $params_arr['address'] = M('midiclub')->where('midiclub_id = '.$exam['exam_midiclub_id'])->getField('midiclub_address');
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80540036');
                        }else{  //线上考试
                            if ($exam['exam_major_id'] == 1) {
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $major = '电贝司';
                            }
                            //考生通知
                            $examtime = M('examtime')->where('examtime_id = '.$exam['exam_time'])->find();
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级的线上考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线上报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'].' '.$examtime['examtime_hi'];
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80475033');
                            //考官通知
                            $examiner = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_examiner_id'])->find();
                            $msg_examiner['message_user_id'] = $exam['exam_examiner_id'];
                            $msg_examiner['message_client'] = 2;
                            $msg_examiner['message_type'] = 2;
                            $msg_examiner['message_title'] = '尊敬的'.$examiner['user_name'];
                            $msg_examiner['message_content'] = '您有一场新的考试'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级,请准时监考！';
                            $msg_examiner['message_time'] = time();
                            M('message')->add($msg_examiner);
                            //考官推送
                            $examiner_content = $msg_examiner['message_title'].':'.$msg_examiner['message_content'];
                            $examiner_value = array('tag'=>array('examiner'.$exam['exam_examiner_id']));
                            push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$exam['exam_id']);
                        }
                    }
                }
                else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //付款完成后，支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $data['exam_num'] = $out_trade_no;
                    $exam = M('exam')->where($data)->find();
                    if ($exam['exam_paystatus'] == 1) { //如果订单未支付
                        $exam['exam_paystatus'] = 2;    //支付成功
                        $exam['exam_status'] = 1;    //报名成功
                        M('exam')->save($exam);    //保存考试信息
                        //考生推送(暂无)
                        //考生通知
                        $user = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_user_id'])->find();

                        if ($exam['exam_midiclub_id'] != -1) {  //如果是线下考试，增加考场人数
                            $condition['examoffline_roomid'] = $exam['exam_midiclub_id'];
                            $condition['examoffline_ymd'] = $exam['exam_ymd'];
                            $offline = M('examoffline')->where($condition)->find();
                            $major = '';
                            if ($exam['exam_major_id'] == 1) {
                                $offline['examoffline_already_guitar'] += 1;
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $offline['examoffline_already_drum'] += 1;
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $offline['examoffline_already_bass'] += 1;
                                $major = '电贝司';
                            }
                            M('examoffline')->save($offline);
                            //考生通知
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].$major.$exam['exam_level'].'级的线下考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线下报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'];
                            $params_arr['address'] = M('midiclub')->where('midiclub_id = '.$exam['exam_midiclub_id'])->getField('midiclub_address');
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80540036');
                        }else{  //线上考试
                            if ($exam['exam_major_id'] == 1) {
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $major = '电贝司';
                            }
                            //考生通知
                            $examtime = M('examtime')->where('examtime_id = '.$exam['exam_time'])->find();
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级的线上考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线上报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'].' '.$examtime['examtime_hi'];
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80475033');
                            //考官通知
                            $examiner = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_examiner_id'])->find();
                            $msg_examiner['message_user_id'] = $exam['exam_examiner_id'];
                            $msg_examiner['message_client'] = 2;
                            $msg_examiner['message_type'] = 2;
                            $msg_examiner['message_title'] = '尊敬的'.$examiner['user_name'];
                            $msg_examiner['message_content'] = '您有一场新的考试'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级,请准时监考！';
                            $msg_examiner['message_time'] = time();
                            M('message')->add($msg_examiner);
                            //考官推送
                            $examiner_content = $msg_examiner['message_title'].':'.$msg_examiner['message_content'];
                            $examiner_value = array('tag'=>array('examiner'.$exam['exam_examiner_id']));
                            push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$exam['exam_id']);
                        }
                    }
                }
                //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                echo "success";     //请不要修改或删除
            // }
            // else //验证签名失败
            // {
            //     echo "sign fail";
            // }
        }
        else //验证是否来自支付宝的通知失败
        {
            echo "response fail";
        }
    }

    /**
     * return_url接收页面
     */
    public function alipay_return(){
        // 引入支付宝
        vendor('Alipay.AlipayNotify','','.class.php');
        $config=C(ALIPAY_CONFIG);
        $notify=new \AlipayNotify($config);
        // 验证支付数据
        $status=$notify->verifyReturn();
        if($status){
            // 下面写验证通过的逻辑 比如说更改订单状态等等 $_GET['out_trade_no'] 为订单号；

            $this->success('支付成功',U('User/Order/index'));
        }else{
            $this->success('支付失败',U('User/Order/index'));
        }
    }

    /**
     * notify_url接收页面
     */
    public function alipay_notify(){
        // 引入支付宝
        vendor('Alipay.AlipayNotify','','.class.php');
        $config=C(ALIPAY_CONFIG);
        $alipayNotify = new \AlipayNotify($config);
        // 验证支付数据
        $verify_result = $alipayNotify->verifyNotify();
        if($verify_result) {
            echo "success";
            // 下面写验证通过的逻辑 比如说更改订单状态等等 $_POST['out_trade_no'] 为订单号；


        }else {
            echo "fail";
        }
    }

    public function test_notify(){
        echo "ali_test_notify";
    }

    public function examorg_notify(){
        vendor('Alipay.alipay_notify','','.class.php');
        $alipay_config = C(ALIPAY_CONFIG);
        $alipayNotify = new \AlipayNotify($alipay_config);
        if($alipayNotify->getResponse($_POST['notify_id']))//判断成功之后使用getResponse方法判断是否是支付宝发来的异步通知。
        {
            // if($alipayNotify->getSignVeryfy($_POST, $_POST['sign'])) {//使用支付宝公钥验签

                //——请根据您的业务逻辑来编写程序（以下代码仅作参考）——
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];

                //支付宝交易号
                $trade_no = $_POST['trade_no'];

                //交易状态
                $trade_status = $_POST['trade_status'];

                if($_POST['trade_status'] == 'TRADE_FINISHED') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //退款日期超过可退款期限后（如三个月可退款），支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                $condition['examorg_enroll_sn'] = $out_trade_no;
                $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
                //如果订单未支付
                if ($examorg_enroll_arr['examorg_enroll_status'] == 1) {
                	//更改报名状态
                	$examorg_enroll_arr['examorg_enroll_status'] = 2;
                	M('examorg_enroll')->save($examorg_enroll_arr);
                	//机构考试人数+1
                	M('examorg')->where(['examorg_id'=>$examorg_enroll_arr['examorg_enroll_examorgid']])->setInc('examorg_enrollnum',1);
                	//考试专业和考试时段字符串转换
                	$exam_major = '';
                	switch ($examorg_enroll_arr['examorg_enroll_major']) {
                		case '1':
                			$exam_major = '电吉他';
                			break;
                		case '2':
                			$exam_major = '爵士鼓';
                			break;
                		case '3':
                			$exam_major = '电贝司';
                			break;
                		default:
                			$exam_major = '未知专业';
                			break;
                	}
                	$exam_time = '';
                	switch ($examorg_enroll_arr['examorg_enroll_time']) {
                		case '1':
                			$exam_time = '上午';
                			break;
                		case '2':
                			$exam_time = '下午';
                			break;
                		case '3':
                			$exam_time = '全天';
                			break;
                		default:
                			$exam_time = '未知时段';
                			break;
                	}
                	//机构信息
                	$midiclub_arr = M('midiclub')->field('midiclub_address,midiclub_name')->where('midiclub_id = '.$examorg_enroll_arr['examorg_enroll_orgid'])->find();
                	//考生通知（1消息 2推送 3短信）
                	$user_arr = M('user')->field('user_id,user_name,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_userid']])->find();
                	//1消息
                	$condition_stu_msg['message_user_id'] = $user_arr['user_id'];
                	$condition_stu_msg['message_client'] = 1;
                	$condition_stu_msg['message_type'] = 2;
                	$condition_stu_msg['message_title'] = '尊敬的'.$user_arr['user_name'];
                	$condition_stu_msg['message_content'] = '恭喜您成功报名'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级的机构考试,请准时参加！';
                    $condition_stu_msg['message_time'] = time();
                	M('message')->add($condition_stu_msg);
                	//2推送
                	$user_content = $condition_stu_msg['message_title'].':'.$condition_stu_msg['message_content'];
                    $user_value = array('tag'=>array('user'.$user_arr['user_id']));
                    push($user_value,$user_content,C(JPUSH_USER),2,$examorg_enroll_arr['examorg_enroll_id']);
                    //3短信
                    $condition_sms['major'] = $exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级';
                    $condition_sms['exam_time'] = $examorg_enroll_arr['examorg_enroll_date'].$exam_major;
                    $condition_sms['address'] = $midiclub_arr['midiclub_address'];
                    $params_json = json_encode($condition_sms);
                    alidayumsg($user_arr['user_phone'],$params_json,'SMS_80540036');
                	//考官通知（1消息 2推送）
                	$examiner_arr = M('user')->field('user_id,user_name,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_examinerid']])->find();
                	//1消息
                	$condition_examiner_msg['message_user_id'] = $examiner_arr['user_id'];
                	$condition_examiner_msg['message_client'] = 2;
                	$condition_examiner_msg['message_type'] = 2;
                	$condition_examiner_msg['message_title'] = '尊敬的'.$examiner_arr['user_name'];
                	$condition_examiner_msg['message_content'] = $midiclub_arr['midiclub_name'].'有新的考试，'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级,请准时监考！';
                    $condition_examiner_msg['message_time'] = time();
                	M('message')->add($condition_examiner_msg);
                	//2推送
                	$examiner_content = $condition_examiner_msg['message_title'].':'.$condition_examiner_msg['message_content'];
                    $examiner_value = array('tag'=>array('examiner'.$examorg_enroll_arr['examorg_enroll_examinerid']));
                    push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$examorg_enroll_arr['examorg_enroll_id']);
                	}
                }
                else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                //判断该笔订单是否在商户网站中已经做过处理
                //如果没有做过处理，根据订单号（out_trade_no）在商户网站的订单系统中查到该笔订单的详细，并执行商户的业务程序
                //如果有做过处理，不执行商户的业务程序
                //注意：
                //付款完成后，支付宝系统发送该交易状态通知
                //请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的
                    $condition['examorg_enroll_sn'] = $out_trade_no;
                $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
                //如果订单未支付
                if ($examorg_enroll_arr['examorg_enroll_status'] == 1) {
                	//更改报名状态
                	$examorg_enroll_arr['examorg_enroll_status'] = 2;
                	M('examorg_enroll')->save($examorg_enroll_arr);
                	//机构考试人数+1
                	M('examorg')->where(['examorg_id'=>$examorg_enroll_arr['examorg_enroll_examorgid']])->setInc('examorg_enrollnum',1);
                	//考试专业和考试时段字符串转换
                	$exam_major = '';
                	switch ($examorg_enroll_arr['examorg_enroll_major']) {
                		case '1':
                			$exam_major = '电吉他';
                			break;
                		case '2':
                			$exam_major = '爵士鼓';
                			break;
                		case '3':
                			$exam_major = '电贝司';
                			break;
                		default:
                			$exam_major = '未知专业';
                			break;
                	}
                	$exam_time = '';
                	switch ($examorg_enroll_arr['examorg_enroll_time']) {
                		case '1':
                			$exam_time = '上午';
                			break;
                		case '2':
                			$exam_time = '下午';
                			break;
                		case '3':
                			$exam_time = '全天';
                			break;
                		default:
                			$exam_time = '未知时段';
                			break;
                	}
                	//机构信息
                	$midiclub_arr = M('midiclub')->field('midiclub_address,midiclub_name')->where('midiclub_id = '.$examorg_enroll_arr['examorg_enroll_orgid'])->find();
                	//考生通知（1消息 2推送 3短信）
                	$user_arr = M('user')->field('user_id,user_name,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_userid']])->find();
                	//1消息
                	$condition_stu_msg['message_user_id'] = $user_arr['user_id'];
                	$condition_stu_msg['message_client'] = 1;
                	$condition_stu_msg['message_type'] = 2;
                	$condition_stu_msg['message_title'] = '尊敬的'.$user_arr['user_name'];
                	$condition_stu_msg['message_content'] = '恭喜您成功报名'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级的机构考试,请准时参加！';
                    $condition_stu_msg['message_time'] = time();
                	M('message')->add($condition_stu_msg);
                	//2推送
                	$user_content = $condition_stu_msg['message_title'].':'.$condition_stu_msg['message_content'];
                    $user_value = array('tag'=>array('user'.$user_arr['user_id']));
                    push($user_value,$user_content,C(JPUSH_USER),2,$examorg_enroll_arr['examorg_enroll_id']);
                    //3短信
                    $condition_sms['major'] = $exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级';
                    $condition_sms['exam_time'] = $examorg_enroll_arr['examorg_enroll_date'].$exam_major;
                    $condition_sms['address'] = $midiclub_arr['midiclub_address'];
                    $params_json = json_encode($condition_sms);
                    alidayumsg($user_arr['user_phone'],$params_json,'SMS_80540036');
                	//考官通知（1消息 2推送）
                	$examiner_arr = M('user')->field('user_id,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_examinerid']])->find();
                	//1消息
                	$condition_examiner_msg['message_user_id'] = $examiner_arr['user_id'];
                	$condition_examiner_msg['message_client'] = 2;
                	$condition_examiner_msg['message_type'] = 2;
                	$condition_examiner_msg['message_title'] = '尊敬的'.$examiner_arr['user_name'];
                	$condition_examiner_msg['message_content'] = $midiclub_arr['midiclub_name'].'有新的考试，'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级,请准时监考！';
                    $condition_examiner_msg['message_time'] = time();
                	M('message')->add($condition_examiner_msg);
                	//2推送
                	$examiner_content = $condition_examiner_msg['message_title'].':'.$condition_examiner_msg['message_content'];
                    $examiner_value = array('tag'=>array('examiner'.$examorg_enroll_arr['examorg_enroll_examinerid']));
                    push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$examorg_enroll_arr['examorg_enroll_id']);
                	}
                }
                //——请根据您的业务逻辑来编写程序（以上代码仅作参考）——
                echo "success";     //请不要修改或删除
            // }
            // else //验证签名失败
            // {
            //     echo "sign fail";
            // }
        }
        else //验证是否来自支付宝的通知失败
        {
            echo "response fail";
        }
    }


}